package edu.software.cakehomework.controller;

import edu.software.cakehomework.entity.Cake;
import edu.software.cakehomework.entity.Type;
import edu.software.cakehomework.service.CakeService;
import edu.software.cakehomework.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/cakes")
@CrossOrigin(origins = "*")
public class CakeController {

    @Autowired
    private CakeService cakeService;

    @Autowired
    private TypeService typeService;

    @GetMapping("/type/{typeId}")
    public ResponseEntity<?> getCakesByType(
            @PathVariable Integer typeId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Type type = typeService.getTypeById(typeId);
            Page<Cake> cakes = cakeService.getCakesByType(type,
                    PageRequest.of(page, size, Sort.by("releaseTime").descending()));
            return ResponseEntity.ok(cakes);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/search")
    public ResponseEntity<?> searchCakes(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<Cake> cakes = cakeService.searchCakes(keyword,
                    PageRequest.of(page, size, Sort.by("releaseTime").descending()));
            return ResponseEntity.ok(cakes);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/popular/{typeId}")
    public ResponseEntity<?> getPopularCakes(@PathVariable Integer typeId) {
        try {
            Type type = typeService.getTypeById(typeId);
            List<Cake> cakes = cakeService.getPopularCakesByType(type);
            return ResponseEntity.ok(cakes);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/discount")
    public ResponseEntity<?> getCakesWithDiscount() {
        try {
            List<Cake> cakes = cakeService.getCakesWithDiscount();
            return ResponseEntity.ok(cakes);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getCakeById(@PathVariable Integer id) {
        try {
            Cake cake = cakeService.getCakeById(id);
            return ResponseEntity.ok(cake);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}